/**
* Copyright @ 2023 iAuto (Shanghai) Co., Ltd.
* All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are NOT permitted except as agreed by
* iAuto (Shanghai) Co., Ltd.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*/



// 服务管理、能力管理可展开组件
import React, { Component } from 'react';
import { Table } from 'antd';
import {
    DownOutlined,
    UpOutlined,
} from '@ant-design/icons';
import { expandTableProps } from '@/types/components'
import styles from './index.less'
export default class ExpandTable extends Component<expandTableProps | any, any>{
    render(): React.ReactNode {
        const {
            dataSource,
            columns,
            rowKey,
            laoding,
            tableProps = {},
            expandable = {},
            childTableProps = {},
            childColumns = [],
            childDataKey = '',
            childDataRowKey = '',
            pagination = {
                current: 1,
                pageSize: 10,
                total: 0,
            },
            // showTotal = (total: number, range: number[]) => `当前显示从 [${range[0]}] 到 [${range[1]}] 总共 ${total} 条记录`,
            showTotal = (total: number) => `总共 ${total} 个项目`,
            ifExpand = () => true
        } = this.props;
        return (
            <div className={styles.expandTable}>
                <Table
                    columns={columns}
                    rowKey={rowKey}
                    dataSource={dataSource}
                    loading={laoding}
                    scroll={{ x: 'max-content' }}
                    expandable={{
                        rowExpandable: ifExpand,
                        expandedRowRender: (record) => {
                            // 处理数据 把父层的 sName 传给子
                            const childDataSource = record[childDataKey]?.map((item: any) => {
                                return {
                                    ...item,
                                    sName: record.sName,
                                }
                            });
                            return (
                                <Table
                                    rowKey={childDataRowKey}
                                    columns={childColumns}
                                    scroll={{ x: 'max-content' }}
                                    pagination={false}
                                    dataSource={childDataSource || []}
                                    {...childTableProps}
                                />
                            );
                        },
                        expandIcon: ({ expanded, onExpand, record }) =>
                            ifExpand(record) ? (
                                expanded ? (<UpOutlined onClick={e => onExpand(record, e)} />) : (<DownOutlined onClick={e => onExpand(record, e)} />)
                            ) : (<span></span>),
                        ...expandable,
                    }}
                    pagination={{
                        size: 'small',
                        showSizeChanger: true,
                        showQuickJumper: true,
                        position: ["bottomRight"],
                        showTotal,
                        ...pagination,
                    }}
                    {...tableProps}
                ></Table>
            </div>
        )
    }
}